package action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;



import dao.OfferDAO;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

import dao.ProductDAO;

public class HomeAction extends ActionSupport implements RequestAware, SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1658169210232618005L;
	
	@Autowired
	private ProductDAO ProductDAO;
	
	@Autowired
	private OfferDAO OfferDAO;
	
	private Map<String, Object> request;
    private Map<String, Object> session;
    
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
	}
	
	public String execute() {
        
		HttpServletRequest _request = ServletActionContext.getRequest();
		
		if (_request.getServletPath().indexOf("/viewalloffer") == 0)
		{
			request.put("offers", OfferDAO.getListOffer());
			return SUCCESS;
		}
		else if ( _request.getServletPath().indexOf("/index") == 0 || _request.getServletPath().equals("/") == true)
		{
			request.put("banner", OfferDAO.getListBanner(0, 3));
			request.put("offers", OfferDAO.getListOffer(0, 6));			
			request.put("products", ProductDAO.ListProdcutOrderByRate(0, 10));
	        return SUCCESS;
		}
		
		return ERROR;
    }
}
